
function updatePage(network,country,station){
  network = typeof network !== 'undefined' ? network : false;
  country = typeof country !== 'undefined' ? country : false;
  station = typeof station !== 'undefined' ? station : false;
  url = '/django/miscellaneous/availability/';
  urlpos = '';
  if(document.getElementById('select_datatype').value != "0"){
   	url += document.getElementById('select_datatype').value+"/";
    form = document.getElementById('form_availability');
    dts = form.daterange.value.split(" - ");
    url += "?dt1="+dts[0]+"&dt2="+dts[1];
    if(network=='all'){
        url += "&network=all";
    }else if (network){
        url += "&network="+network;
        urlpos = '#networks';
    }
    if(country=='all'){
        url += "&country=all";
    }else if (country){
        url += "&country="+country;
        urlpos = '#countries';
    }
    if(station=='all'){
        url += "&station=all";
    }else if (station){
        url += "&station="+station;
        urlpos = '#stations';
    }
  }
  location.href = url+urlpos;
    
};          


function updateNetworks(){
  if($('#all_networks').is(':checked')){
    updatePage('all');
  }
};
function updateCountries(){
  if($('#all_countries').is(':checked')){
    vars = getUrlVars();
    updatePage(vars['network'],'all');
  }
};
function updateStations(){
  if($('#all_stations').is(':checked')){
    vars = getUrlVars();
    updatePage(vars['network'],vars['country'],'all');
  }
};

function updateNetwork(check){
  if(!(check.checked)){
    $('#all_networks').prop('checked', false);
  }
  updatePage($("input[name=ch_network]:checked").map(function(){return this.value;}).get().join(","));
};
function updateCountry(check){
  if(!(check.checked)){
    $('#all_countries').prop('checked', false);
  }
  vars = getUrlVars();
  updatePage(vars['network'],$("input[name=ch_country]:checked").map(function(){return this.value;}).get().join(","));
};
function updateStation(check){
  if(!(check.checked)){
    $('#all_stations').prop('checked', false);
  }
  vars = getUrlVars();
  updatePage(vars['network'],vars['country'],$("input[name=ch_station]:checked").map(function(){return this.value;}).get().join(","));
};

function showPlot(){

    if($('#select_datatype').val() == 0){
        $("#div_alert_content").html('<strong>Warning!</strong> You have not slected any data type');
        $(".alert").show();
    }else{
        $('#myModal').modal({
          show: true
        });
    }
};

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value.split('#')[0];
    });
    return vars;
}

